package puyu.manage.service.report.service.impl;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import puyu.manage.service.common.dao.CommonMapper;
import puyu.manage.service.file.entity.SysTFileUpload;
import puyu.manage.service.file.entity.vo.FileUploadVo;
import puyu.manage.service.file.service.SysTFileCenterService;
import puyu.manage.service.file.service.SysTFileUploadService;
import puyu.manage.service.report.dao.ReportDao;
import puyu.manage.service.report.service.ReportService;
import org.springframework.stereotype.Service;
import puyu.manage.service.utils.CommonUtil;

import javax.annotation.Resource;
import java.util.*;

/**
 *
 * @author whl
 * @since 2024-06-17 18:24:42
 */
@Service("puyuTReportService")
public class ReportServiceImpl implements ReportService {

    @Resource
    private ReportDao reportDao;

    @Autowired(required = false)
    CommonMapper commonMapper;

    @Resource
    private SysTFileUploadService sysTFileUploadService;

    @Resource
    private SysTFileCenterService sysTFileCenterService;

    /**
     * @Description 查询单条数据
     * @param reportId 主键
     * @return 实例对象
     */
    @Override
    public Map<String,Object> queryById(String reportId) {
        Map<String,Object> datas=reportDao.queryById(reportId);
        List<Map<String,Object>> sampleData=commonMapper.queryListBySql("select s.sample_id,s.sample_code,s.sample_name,s.sample_part,s.init_check,s.sample_level,s.test_type,s.sample_code_path,s.scan_num,s.sample_code_create_time,s.code_status,s.entrust_time,s.video_path,s.video_status,s.video_content,DATE_FORMAT(s.video_start_time,'%Y-%m-%d %H:%i:%s') video_start_time,DATE_FORMAT(s.video_end_time,'%Y-%m-%d %H:%i:%s') video_end_time,s.is_del,s.status,s.connection,s.sample_kind,s.specification,s.device_name,s.device_serial,s.local_index,s.create_time,s.create_by,s.update_time,s.update_by,s.remark," +
                "(select GROUP_CONCAT(pt.task_code) from puyu_t_task pt where pt.task_id in (select pts.task_id from puyu_t_task_sample pts where pts.sample_id =s.sample_id)) tsak_code " +
                "from puyu_t_sample s where sample_id in (select sample_id from puyu_t_task_sample where  task_id in (select task_id from puyu_t_report_task where report_id ='"+reportId+"'))");
        datas.put("samples",sampleData);
        return datas;
    }


    /**
     * @Description 分页查询
     * @author whl
     */
    @Override
    public List<Map<String,Object>> getPageList(Map<String,Object> params) {
        List<Map<String,Object>> datas=reportDao.getTableList(params);
        for(Iterator<Map<String, Object>> it = datas.iterator(); it.hasNext();){
            Map<String, Object> data =it.next();
            List<String> taskCodes=commonMapper.queryStringListBySql("SELECT task_code from puyu_t_task where task_id in (SELECT task_id from puyu_t_report_task where report_id ='"+data.get("reportId")+"')");
            data.put("taskCodes",taskCodes);
        }
        return datas;
    }


    /**
     * 新增数据
     *
     * @return 实例对象
     */
    @Override
    public Map<String,Object> insertTableData(Map<String,Object> params) {
        reportDao.insertTableData(params);
        return null;
    }



    /**
     * @Description 修改数据，哪个属性不为空就修改哪个属性
     * @return 实例对象
     */
    @Override
    public Map<String,Object> editReportCode(Map<String,Object> params) {
        String id =  params.get("reportId")+"";
        MultipartFile file = CommonUtil.imageConvert(params.get("file")+"");
        //判空
        if (ObjectUtil.isNotEmpty(file)){

            //删除之前数据
            sysTFileCenterService.deleteByFile(id,"2","report");

            //上传文件到Minio
            SysTFileUpload upload = sysTFileUploadService.upload(file);
            FileUploadVo vo = new FileUploadVo();
            vo.setId(id);
            List<String> ids = new ArrayList<>();
            ids.add(upload.getFileId());
            vo.setFileIds(ids);
            //文件关联表
            sysTFileCenterService.saveByFile(vo);

            //二维码地址
            params.put("reportCodePath",upload.getFileUrl());
            //二维码生成时间
            params.put("reportCodeCreateTime", new Date());
            //二维码扫码次数默认为0
            params.put("reportCodeScanNum", "0");
        }
        //修改报告二维码地址、生成时间，报告二维码扫码次数
        reportDao.editTableData(params);
        return null;
    }

    /**
     * @Description 删除数据
     * @author whl
     * @param reportId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String reportId) {
        if(reportDao.deleteById(reportId) == 1){
            return true;
        }
        return false;
    }

    @Override
    public String updateReportNum(String id) {
        commonMapper.executeSql("update puyu_t_report set report_code_scan_num=report_code_scan_num+1 where report_id= '"+id+"'");
        return null;
    }

    @Override
    public String queryTaskVideoById(String id) {
        String tasks="";
        tasks=commonMapper.queryString("select group_concat(task_code) from puyu_t_task pt where  task_id in (select task_id from puyu_t_report_task where report_id ='"+id+"') and EXISTS(SELECT 1 from puyu_t_task_sample pts where pts.task_id=pt.task_id and  pts.sample_id in (\n" +
                "select sample_id from puyu_t_sample where video_path is null))");
        return tasks;
    }
}
